package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class OrderReciever {
    @Autowired
    private OrderInfoService orderInfoService;


    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER ),
            value = @Queue(value = MqConst.QUEUE_ORDER_PAY_SUCCESS, durable = "true",autoDelete = "false"),
            key = {MqConst.ROUTING_ORDER_PAY_SUCCESS}
    ))
    public void orderPaySuccess(String orderNo, Message message, Channel channel) {
        //业务处理
        if (!StringUtils.isEmpty(orderNo)) {
            //监听当前订单号不为空 更新订单状态 1402 已支付  记录用户的购买记录
            orderInfoService.orderPaySuccess(orderNo);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @SneakyThrows
    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void orderCancel(String orderNo, Channel channel, Message message) {
        if (null != orderNo) {
        orderInfoService.orderCancel(orderNo);
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
